package cn.itcast.bos.web.action.page;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.CourierService;
import cn.itcast.bos.web.action.commen.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@ParentPackage(value = "json-default")
@Namespace("/")
@Scope("prototype")
public class CourierPageAction extends BaseAction<Courier> {

    @Autowired
    private CourierService courierService;

    //有条件分页查询
    @Action(value = "courier_pageSpecificQuery", results = {@Result(name = "success", type = "json")})
    public String pageSpecificQuery() {
        // 封装Pageable对象
        Pageable pageable = new PageRequest(page - 1, rows);
        //设置条件
        Specification<Courier> specification = new Specification<Courier>() {
            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                if (StringUtils.isNotBlank(model.getCourierNum())) {
                    Predicate p1 = cb.equal(root.get("courierNum").as(String.class), model.getCourierNum());
                    list.add(p1);
                }
                if (StringUtils.isNotBlank(model.getCompany())) {
                    Predicate p2 = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
                    list.add(p2);
                }
                if (StringUtils.isNotBlank(model.getType())) {
                    Predicate p3 = cb.equal(root.get("type").as(String.class), model.getType());
                    list.add(p3);
                }
                // 多表查询
                Join<Courier, Standard> standardJoin = root.join("standard", JoinType.INNER);
                if (model.getStandard() != null &&
                        StringUtils.isNotBlank(model.getStandard().getName())) {
                    Predicate p4 = cb.like(
                            standardJoin.get("name").as(String.class), "%"
                                    + model.getStandard().getName() + "%");
                    list.add(p4);
                }

                return cb.and(list.toArray(new Predicate[0]));
            }
        };

        Page<Courier> pageData = courierService.findPageData(specification, pageable);
       pushPageDataToValueStack(pageData);
        return SUCCESS;
    }
    //无条件查询分页
    @Action(value = "courier_pageQuery", results = {@Result(name = "success", type = "json")})
    public String pageQuery(){
        // 封装Pageable对象
        Pageable pageable = new PageRequest(page - 1, rows);
        Page<Courier> pageData=courierService.findPageData(pageable);
        pushPageDataToValueStack(pageData);
        return SUCCESS;
    }
    @Action(value = "courier_findAll",results = {@Result(name = "success",type = "json")})
    public String findAll(){
        List<Courier> courier= courierService.findAll();
        ActionContext.getContext().getValueStack().push(courier);
        return SUCCESS;
    }


}
